#include <stdio.h>
#include <stdlib.h>

int heapify();
int create_max_heap();
int forwardSort();
int backwardSort();

#define ERROR_RETURN -1
#define SUCCESS_RETURN 0

#define debug(...)  printf(__VA_ARGS__)
#define error(...)  do {\
                        printf(__VA_ARGS__);\
                        exit(ERROR_RETURN);\
                    }while(0);

struct Heap {
    int *data;
    int heap_size;
};

typedef struct Heap Heap;

int heapify(int *array_to_sort, int position, int heapsize) {
    
}

void usage(char *binary_name) {
    printf("\n Incorrect usage: Please re-enter the command in following "\
        "format\n");
    printf("\n%s <first number> <second number> <third number> ....\n", \
        binary_name);
    return;
}

void print_heap_element(Heap *h) {
    /* Print the number of elements in the heap structure, in whatever form
     * they are
     */
    int i;

    printf("Heap Data: ");
    for (i = 0; i < h->heap_size; i++) {
        printf(" %d ", h->data[i]);
    }
    printf("\n");
}

void populate_Heap(Heap *ptr, int *ptr_to_data, int size) {
    ptr->data = ptr_to_data;
    ptr->heap_size = size;
}

int main(int argc, char **argv) {
    int *data_to_sort = NULL;
    int counter = 0;
    int cValue = 0;
    Heap h = { NULL, 0 };

    /* get the array from the user */
    if (argc <= 1) {
        /* User hasn't entered any set of numbers to be formatted */
        usage(argv[0]);
        return ERROR_RETURN;
    }

    /* Allocate enough memory to hold what user has entered
     * For each number entered by user, convert into integer */
    debug("Allocating space for %d entries\n", (argc - 1));
    data_to_sort = (int *)malloc(sizeof(int) * (argc - 1));
    if (data_to_sort == NULL) {
        /* Unable to allocate enough memory */
        error("Unable to allocate enough memory to work. Aborting!\n");
    }
    
    /* Memory has been allocated, filling in the array to be sorted */
    for (counter = 1; counter <= (argc - 1); counter++) {
        data_to_sort[counter - 1] = atoi(argv[counter]);
        /* TODO above line ignores the case where in user enters gibberish
         * delibrately. Should be fixed!
         */
    }
    debug("Populated array is:");
    /* XXX Only for debugging purpose */
    counter = 0;
    while (counter < argc - 1) {
        debug(" %d ", data_to_sort[counter]);
        counter++;
    }
    debug("\n");
    /* End of debugging block */

    /* for the data collected, heapify has to be called once to take the
     * biggest element to top (MAX HEAP)
     */
    debug("Populating the Heap Structure on which sorting would be done\n");
    populate_Heap(&h, data_to_sort, argc - 1);
    /* XXX Only for debugging purpose */
    print_heap_element(&h);
    /* End of debug data */

    /* we are ready for sorting in ascending order */
    /* TODO, restructring of the program is required to accept from the user
     * what he/she would like to do - ascending or descending sort
     */
    debug("Calling forward sorting function \n");
    //forward_sort(&h);



    return 0;
}
